$(document).ready(function(){
	
	$('#putAccount').focus();
	
	$("#putAccount").keypress(function (event) {
		if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
	       return false;
	    }
	});
	
	// เมื่อแก้ไข เลขอ่านมาตรครั้งปัจจุบัน
	$("#currentReadNumber").keypress(function (event) {
		if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
	       return false;
	    }
	});

	$('#currentReadNumber').keyup(function(){
		var consumption;
		var currentRead = $("#currentReadNumber").val();
		var previousRead = $("#previousReadNumber").text();
		consumption = parseFloat(currentRead) -  parseFloat(previousRead);
		$("#consumption").val(consumption);
    });
	
	$("#consumption").keypress(function (event) {
		if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
	       return false;
	    }
	});
	
	// เมื่อแก้ไข ค่าน้ำ
	$("#waterCharge").keypress(function (event) {
		if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
	       return false;
	    }
	});
	
	$('#waterCharge').keyup(function(){
		calTextChargeService();
	});
	
	//เมื่อแก้ไข ค่าบริการ
	$("#waterFee").keypress(function (event) {
		if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
	       return false;
	    }
	});
	
	$('#waterFee').keyup(function(){
		calTextChargeService();
	});
	
	//เมื่อแก้ไข ค่าภาษี
	$("#vatAmount").keypress(function (event) {
		if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
	       return false;
	    }
	});
	
	$('#vatAmount').keyup(function(){
		var billAmount;
		var totalAmount;
		var waterCharge = $("#waterCharge").val();
		var waterFee = $("#waterFee").val();
		var vatAmount = $("#vatAmount").val();
		var garbageCharge = $("#garbageCharge").val();
		var wasteWaterCharge = $("#wasteWaterCharge").val();
		var otherCharge = $("#otherCharge").val();
		billAmount = parseFloat(waterCharge.replace(",", "")) + parseFloat(waterFee) + parseFloat(vatAmount);
		totalAmount = parseFloat(garbageCharge) + parseFloat(wasteWaterCharge) + parseFloat(otherCharge) + parseFloat(billAmount);
		$("#billAmount").val(format(billAmount,2));
		$("#totalAmount").val(format(totalAmount,2));
	});
	
	// เมื่อแก้ไข ค่าเก็บขยะ
	$("#garbageCharge").keypress(function (event) {
		if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
	       return false;
	    }
	});

	$('#garbageCharge').keyup(function(){
		$("#totalAmount").val(calTextCharge($('#garbageCharge').val(), "GC"));
	});
	
	// เมื่อแก้ไข ค่าบำบัดน้ำเสีย
	$("#wasteWaterCharge").keypress(function (event) {
		if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
	       return false;
	    }
	});

	$('#wasteWaterCharge').keyup(function(){
		$("#totalAmount").val(calTextCharge($('#wasteWaterCharge').val(), "WC"));
	});
	
	// เมื่อแก้ไข ค่าอื่นๆ
	$("#otherCharge").keypress(function (event) {
		if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
	       return false;
	    }
	});

	$('#otherCharge').keyup(function(){
		$("#totalAmount").val(calTextCharge($('#otherCharge').val(), "OC"));
	});
	
	$("#totalAmount").keypress(function (event) {
		if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
	       return false;
	    }
	});
});

// เมื่อกดปุ่ม Enter
function Enter(e) {
	if (e.keyCode == 13) {
		searchAccount();
	  return false;  
	}
};

//Function ยืนยันข้อมูลการบันทึกใบแจ้งหนี้
function confirm(){
	var account = $("#account").text();
	if (account == "") {
		$('#checkResultModal').modal();
	} else {
		$('#confirmSaveModal').modal();
	}
}

//Function บันทึกข้อมูลใบแจ้งหนี้
function save(){
	var account = $("#account").text();
	var consumption = $("#consumption").val();
	var currentReadDate = $("#datetimepicker1").val();
	var currentReadNumber = $("#currentReadNumber").val();
	var waterCharge = $("#waterCharge").val();
	var waterFee = $("#waterFee").val();
	var vatAmount = $("#vatAmount").val();
	var billAmount = $("#billAmount").val();
	var garbageCharge = $("#garbageCharge").val();
	var wasteWaterCharge = $("#wasteWaterCharge").val();
	var otherCharge = $("#otherCharge").val();
	var allowance = "";
	if ($("#allowance").text() != ""){
		allowance = "1";
	} else {
		allowance = "0";
	}
	var totalAmount = $("#totalAmount").val();
	var inputdoReason = $("#inputdoReason").val();
	$.ajax({           	
		url : "CS42001Srvl",
		type : "POST",
		data : {methodType: "saveData", 
			account:account,
			consumption:consumption,
			currentReadDate:currentReadDate,
			currentReadNumber:currentReadNumber,
			waterCharge:waterCharge,
			waterFee:waterFee,
			vatAmount:vatAmount,
			billAmount:billAmount,
			garbageCharge:garbageCharge,
			wasteWaterCharge:wasteWaterCharge,
			otherCharge:otherCharge,
			allowance:allowance,
			totalAmount:totalAmount,
			inputdoReason:inputdoReason,
			url_address:$("#url_address").val(),
			agent_browser:$("#agent_browser").val()
		},
		async : false ,
		success : function(data) {
			var newdata = jQuery.parseJSON(data);
			if (newdata.message == "success") {
				$('#confirmSaveModal').modal('hide');
				$('#putAccount').val("");
				$('#successModal').modal();
			} else {
				$('#confirmSaveModal').modal('hide');
				$('#failModal').modal();
			}
			
		} 
	});
}

//Function ค้นหา ทะเบียนผู้ใช้น้ำ
function searchAccount(){
	var account = $("#putAccount").val();
	if (account == ""){
		$('#failAccountModal').modal();
	} else {
		$.ajax({           	
    		url : "CS42001Srvl",
    		type : "POST",
    		data : {methodType:"getDataAR",
    			account:account,
    			url_address:$("#url_address").val(),
    			agent_browser:$("#agent_browser").val()},
    		async : false ,
    		success : function(data) {
    			var newdata = jQuery.parseJSON(data);
    			if (newdata.message == "resultNull"){
    				$('#messageBoxSearch').text("ไม่พบข้อมูลทะเบียนผู้ใช้น้ำนี้");
    				$('#failResultModal').modal();
    			} else if (newdata.message == "status") {
    				$('#messageBoxSearch').text("ไม่พบข้อมูลทะเบียนผู้ใช้น้ำนี้ เนื่องจาก ยังไม่ได้ถูกยกเลิก");
    				$('#failResultModal').modal();
    			} else {
        			$("#account").text(newdata.account);
        			$("#zoneName").text(newdata.zoneName);
        			$("#routeDescription").text(newdata.routeDescription);
        			$("#sequence").text(newdata.sequence);
        			$("#fullName").text(newdata.fullName);
        			$("#customerClass").text(newdata.customerClass);
        			$("#customerClassId").val(newdata.customerClassId);
        			$("#address").text(newdata.address);
        			if (newdata.allowance == "1"){
        				$("#allowance").text("มีส่วนลดทหารผ่านศึก");
        			} else {
        				$("#allowance").text("");
        			}
        			$("#previousReadDate").text(newdata.previousReadDate);
        			$("#datetimepicker1").val(newdata.currentReadDate);
        			$("#previousReadNumber").text(newdata.previousReadNumber);
        			$("#currentReadNumber").val(newdata.currentReadNumber);
        			$("#consumption").focus();
        			$("#consumption").val("0");
        			$("#waterCharge").val("0.00");
        			$("#waterFee").val("0.00");
        			$("#vatAmount").val("0.00");
        			$("#billAmount").val("0.00");
        			$("#garbageCharge").val("0.00");
        			$("#wasteWaterCharge").val("0.00");
        			$("#otherCharge").val("0.00");
        			$("#totalAmount").val("0.00");
        			$("#inputdoReason").val("");
    			}
    		}
		});	
	}
}

//Function คำนวณค่าน้ำทั้งหมด
function calConsumption(){
	var account = $("#account").text();
	var typeCal = $("input:radio[name=typeCal]:checked").val();
	var consumption = $("#consumption").val();
	var currentReadDate = $("#datetimepicker1").val();
	var previousReadDate = $("#previousReadDate").text();
	var customerClass = $("#customerClassId").val();
	var allowance = "";
	if ($("#allowance").text() != ""){
		allowance = "1";
	} else {
		allowance = "0";
	}
	$.ajax({
		url : "CS42001Srvl",
		type : "POST",
		data : {
			methodType:"calConsumption", 
			account:account,
			consumption:consumption,
			typeCal:typeCal, 
			currentReadDate:currentReadDate, 
			previousReadDate:previousReadDate,
			customerClass:customerClass,
			allowance:allowance
		},
		async : false ,
		success : function(data) {
			var newdata = jQuery.parseJSON(data);
				$("#consumption").val(newdata.consumption);
				$("#waterCharge").val(newdata.waterChargeNew);
				calTextChargeService();
			}
	});
}

// Function เมื่อแก้ไข ค่าน้ำ หรือค่าบริการ
function calTextChargeService(){
	var billAmount;
	var totalAmount;
	var waterCharge = $("#waterCharge").val();
	var waterFee = $("#waterFee").val();
	var garbageCharge = $("#garbageCharge").val();
	var wasteWaterCharge = $("#wasteWaterCharge").val();
	var otherCharge = $("#otherCharge").val();
	
	$.ajax({           	
		url : "CS42001Srvl",
		type : "POST",
		data : {methodType: "getVatRate", waterCharge:waterCharge, waterFee:waterFee, url_address:$("#url_address").val(), agent_browser :$("#agent_browser").val()},
		async : false ,
		success : function(data) {
			var newdata = jQuery.parseJSON(data);
			billAmount = parseFloat(waterCharge.replace(",", "")) + parseFloat(waterFee) + parseFloat(newdata.vatAmountNew);
			totalAmount = parseFloat(garbageCharge) + parseFloat(wasteWaterCharge) + parseFloat(otherCharge) + parseFloat(billAmount);
			$("#vatAmount").val(newdata.vatAmountNew);
			$("#billAmount").val(format(billAmount,2));
			$("#totalAmount").val(format(totalAmount,2));
		}
	});	
}

// Function แกไข ค่าเก็บขยะ ค่าบำบัดน้ำเสีย ค่าอื่นๆ
function calTextCharge(value1, value2){
	var billAmount;
	var totalAmount;
	var garbageCharge;
	var wasteWaterCharge;
	var otherCharge;
	if (value2 == "GC"){
		garbageCharge = value1;
	} else {
		garbageCharge = $("#garbageCharge").val();
	}
	if (value2 == "WC") {
		wasteWaterCharge = value1;
	} else {
		wasteWaterCharge = $("#wasteWaterCharge").val();
	}
	if(value2 == "OC"){
		otherCharge = value1;
	} else {
		otherCharge = $("#otherCharge").val();
	}
	billAmount = $("#billAmount").val();
	totalAmount = parseFloat(garbageCharge.replace(",", "")) + parseFloat(wasteWaterCharge.replace(",", "")) + parseFloat(otherCharge.replace(",", "")) + parseFloat(billAmount.replace(",", ""));
	
	return format(totalAmount,2);
}

// รูปแบบ ตัวเลข
function format(num, fix) {
    var p = num.toFixed(fix).split(".");
    return p[0].split("").reduceRight(function(acc, num, i, orig) {
        if ("-" === num && 0 === i) {
            return num + acc;
        }
        var pos = orig.length - i - 1
        return  num + (pos && !(pos % 3) ? "," : "") + acc;
    }, "") + (p[1] ? "." + p[1] : "");
}

//Function เคลียร์หน้าจอ
function clearScreen(){
	$("#account").text("");
	$("#zoneName").text("");
	$("#address").text("");
	$("#routeDescription").text("");
	$("#sequence").text("");
	$("#fullName").text("");
	$("#customerClass").text("");
	$("#address").text("");
	$("#allowance").text("");
	$("#previousReadDate").text("");
	$("#datetimepicker1").val("");
	$("#previousReadNumber").text("");
	$("#currentReadNumber").val("");
	$('input[name=typeCal][value=G]').prop('checked', 'checked');
	$("#consumption").val("0");
	$("#waterCharge").val("0.00");
	$("#waterFee").val("0.00");
	$("#vatAmount").val("0.00");
	$("#billAmount").val("0.00");
	$("#garbageCharge").val("0.00");
	$("#wasteWaterCharge").val("0.00");
	$("#otherCharge").val("0.00");
	$("#totalAmount").val("0.00");
	$("#inputdoReason").val("");
}

//Function ปิด Popup ค้นหา
function getFocus(){
	$(".close").trigger('click');
	$('#putAccount').focus();
	clearScreen();
}

//Function ปิด Popup ยืนยัน
function cancel(){
	$(".close").trigger('click');
	$('#consumption').focus();
}

	